<template>
  <el-card>

    <el-table border style="margin: 10px 0" :data="skuArr">
      <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column label="名称" prop="skuName" show-overflow-tooltip width="200px"></el-table-column>
      <el-table-column label="描述" prop="skuDesc" show-overflow-tooltip width="200px"></el-table-column>
      <el-table-column label="默认图片" width="250px" align="center">
        <template #="{row}">
          <img :src="row.skuDefaultImg" alt="" width="100px" height="100px">
        </template>
      </el-table-column>
      <el-table-column label="重量(g)" prop="weight" width="200px"></el-table-column>
      <el-table-column label="价格(元)" prop="price" width="200px"></el-table-column>
      <el-table-column label="操作" width="350px" fixed="right">
        <template #="{row}">
          <el-button :type="row.isSale==1?'warning':'success'" :icon="row.isSale==1?'Bottom':'top'"
                     size="small" @click="updateSale(row)"></el-button>
          <el-button type="primary" icon="Edit" size="small" @click="edit"></el-button>
          <el-button type="info" icon="InfoFilled" size="small" @click="skuDetail(row)"></el-button>
          <el-popconfirm :title="`你确定要删除${row.skuName}吗?`" @confirm="removeSku(row)" width="200px">
            <template #reference>
              <el-button type="danger" icon="Delete" size="small"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--分页器-->
    <el-pagination @current-change="getHasSku" @size-change="handler"
                   :current-page="pageNo" :page-size="limit" :page-sizes="[10,20,30,40]"
                   :background="true" layout="prev,pager,next,jumper,->,sizes,total" :total="total"/>

    <!--抽屉组件：展示商品详情-->
    <el-drawer v-model="drawer">
      <!--标题-->
      <template #header>
        <h4>查看商品详情</h4>
      </template>

      <!--主体-->
      <template #default>
        <el-row style="margin: 10px 0">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuDetailInfo.skuName }}</el-col>
        </el-row>

        <el-row style="margin: 10px 0">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuDetailInfo.skuDesc }}</el-col>
        </el-row>

        <el-row style="margin: 10px 0">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuDetailInfo.price }}</el-col>
        </el-row>

        <el-row style="margin: 10px 0">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag v-for="(item) in skuDetailInfo.skuAttrValueList" :key="item.id" style="margin: 0 5px">
              {{ item.valueName }}
            </el-tag>
          </el-col>
        </el-row>

        <el-row style="margin: 10px 0">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag v-for="(item) in skuDetailInfo.skuSaleAttrValueList" :key="item.id"
                    style="margin: 0 5px" type="warning">{{ item.saleAttrValueName }}
            </el-tag>
          </el-col>
        </el-row>

        <el-row style="margin: 10px 0">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item v-for="item in skuDetailInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" alt="" width="100%" height="100%">
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>


      </template>

    </el-drawer>

  </el-card>
</template>

<script setup lang="ts">
import {ref, onMounted} from "vue";
import {
  reqSkuListInfo,
  reqSaleSku,
  reqCancelSaleSku,
  reqSkuDetailInfo,
  reqRemoveSku
//@ts-ignore
} from "@/api/product/sku/skuApi.ts";
//@ts-ignore
import type {T_SkuData, T_SkuInfoResponseData, T_SkuResponseData} from "@/api/product/sku/type.ts";
import {ElMessage} from "element-plus";

//分页器
let pageNo = ref<number>(1); //当前页
let limit = ref<number>(10); //一页几条数据
let total = ref<number>(0);
let skuArr = ref<T_SkuData[]>([])
//抽屉
let drawer = ref<boolean>(false);//控制显示/隐藏
//sku详情数据
let skuDetailInfo = ref<T_SkuData>({});


//组件挂载完毕，获取第一页请求
onMounted(() => {
  getHasSku();
})

//获取数据
const getHasSku = async (pager = 1) => {
  pageNo.value = pager;
  let result: T_SkuResponseData = await reqSkuListInfo(pageNo.value, limit.value);
  if (result.code == 200) {
    total.value = result.data.total;
    skuArr.value = result.data.records;
  } else {
    ElMessage({
      type: "error",
      message: `获取第${pageNo.value}页的sku信息失败`
    })
  }
}

//分页器，下拉菜单发生变化时 的回调
const handler = async (pageSize: number) => {
  limit.value = pageSize;
  getHasSku();
}

//商品的上架/下架
const updateSale = async (sku: T_SkuData) => {
  //isSale==1 说明是上架状态
  if (sku.isSale == 1) {
    //下架
    let result = await reqCancelSaleSku(sku.id);
    if (result.code == 200) {
      ElMessage({
        type: "success",
        message: "下架成功"
      })
      getHasSku(pageNo.value);
    } else {
      ElMessage({
        type: "error",
        message: "下架失败"
      })
    }
  } else {
    let result = await reqSaleSku(sku.id);
    //上架
    if (result.code == 200) {
      ElMessage({
        type: "success",
        message: "上架成功"
      })
      getHasSku(pageNo.value);
    } else {
      ElMessage({
        type: "error",
        message: "上架失败"
      })
    }
  }
}

//编辑sku
const edit = () => ElMessage({type: "success", message: "该模块待开发..."});

//查看sku
const skuDetail = async (sku: T_SkuData) => {
  let result: T_SkuInfoResponseData = await reqSkuDetailInfo(sku.id);
  if (result.code == 200) {
    drawer.value = true;
    skuDetailInfo.value = result.data;
  } else {
    ElMessage({
      type: "error",
      message: "获取详情信息失败"
    })
  }
}

//删除sku回调
const removeSku = async (sku: T_SkuData) => {
  let result = await reqRemoveSku(sku.id);
  if (result.code == 200) {
    ElMessage({type: "success", message: `成功删除${sku.skuName}`})
    //重新获取数据
    getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1);
  } else {
    ElMessage({type: "error", message: `成功失败${sku.skuName}`})
  }
}

</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>